//
//  PredictNetRequest.m
//  Prophecy
//
//  Created on 14-7-29.
//  Copyright (c) 2014年. All rights reserved.
//

#import "AProjectNetRequest.h"
#import "ServiceConfig.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonHMAC.h>

@implementation AProjectNetRequest

@synthesize requestInterface;
@synthesize responseEntity;
@synthesize entityName;

- (NSString*) requestInterface {
    
    // 从配置器中获取当前接口请求的接口方法名称
    NSString *method = [SC getRequestInterfaceForInterfacekey:self.requestTypeKey];
    
    if ( method == nil || method.length == 0 ) {
        return @"";
    }
    
    return method;
}

- (NSString*) entityName
{
    // 从配置器中获取当前接口请求的接口方法名称
    NSDictionary *table = [SC getAPIConfigForInterfacekey:self.requestTypeKey];
    
    NSString *entityClassName = [table objectForKey:@"entityObject"];
    
    if ( entityClassName == nil || entityClassName.length == 0 ) {
        return @"";
    }
    
    return entityClassName;
}

- (SEL) entitParsersel
{
    // 从配置器中获取当前接口请求的接口方法名称
    NSDictionary *table = [SC getAPIConfigForInterfacekey:self.requestTypeKey];
    
    NSString *entitParserselName = [table objectForKey:@"ParserName"];
    
    if ( entitParserselName == nil || entitParserselName.length == 0 ) {
        return nil;
    }
    
    return NSSelectorFromString(entitParserselName);
}

// 获得当前请求对像的唯一标识
- (NSString*) getRequestUUID
{
    NSString *uuidkey = @"";

    // 添加签名
    NSMutableDictionary * postDic = [NSMutableDictionary dictionaryWithDictionary:self.parameterEntity.postDictionary];
    NSMutableDictionary * getDic = [NSMutableDictionary dictionaryWithDictionary:self.parameterEntity.getDictionary];
    NSMutableDictionary * headDic = [NSMutableDictionary dictionaryWithDictionary:self.parameterEntity.headDictionary];
    
    NSMutableDictionary *signtable = [NSMutableDictionary dictionary];
    [signtable addEntriesFromDictionary:postDic];
    [signtable addEntriesFromDictionary:getDic];
    [signtable addEntriesFromDictionary:headDic];
    
    uuidkey = [self signature:signtable];
    
    return uuidkey;
}

- (NSString*) signature:(NSDictionary*) posttable
{
    __autoreleasing NSString *signature = nil;
    
    __autoreleasing NSString *string = @"";
    
    NSArray *ascendingKey = [self sortArrayDesc:[posttable allKeys]];
    
    for ( NSString *key in ascendingKey ) {
        
        id obj = [posttable objectForKey:key];
        
        if ( [key isEqualToString:@"sign"] || [key isEqualToString:@"timestamp"] || [key isEqualToString:@"image"] || [key isEqualToString:@"audio"] || [key isEqualToString:@"predicttime"] || [obj description].length == 0 ) {
            continue;
        }
        
        string = [NSString stringWithFormat:@"%@%@%@", string, key, obj];
    }
    
    signature = [[NSString alloc] initWithFormat:@"%@", [self MD5EncodedString:string]];
    
    
    return signature;
}


/**
 @method 数组升序排序
 @param array 排序的数组
 */
-(NSArray *)sortArrayDesc:(NSArray *)array{
    NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2){
        return [obj1 compare:obj2];
    }];
    return sortedArray;
}

/**
 @method MD5加密
 @param content 加密的字符串
 @result 返回加密字符串
 */
- (NSString *)MD5EncodedString:(NSString *)content{
    const char *str = [content UTF8String];
    unsigned char r[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (CC_LONG)strlen(str), r);
    NSString *filename = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                          r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
    return filename;
}


@end
